package bug
{
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import utils.pool.IRecyclable;
	import utils.pool.ObjectPool;

	public class Ant extends Sprite implements IRecyclable
	{
		private var targetPostion:Point;
		private var angel:Number;
		internal var _speed:Number=1.6;
		private var _isPlaying:Boolean;
		internal var _blood:Number=10;
		private var _isDispose:Boolean=false;
		
		public static const antPool:ObjectPool = new ObjectPool(Ant);
		private var _power:int;
		private var _home:Castle;
		public function Ant()
		{
			
		}
        public function checkAntHit(ant:Ant):Boolean{
			return Math.sqrt(Math.pow(x-ant.x,2)+Math.pow(y-ant.y,2))<10;
			return false;		}
		public function get home():Castle
		{
			return _home;
		}

		public function get isDead():Boolean
		{
			return _isDead;
		}

		public function get power():int
		{
			return _power;
		}
		private var _display:Image;
	    public function reset(power:int):Ant{
			_isDispose=false;
			_power=power;
			if(_display==null){
				_display = ResourcesManager.getBitmap("bugs/b2.png");
				_display.scaleX=-1;
				
				addChild(_display);
			}
			return this;
		}
		internal function setHome(home:Castle):void{
			_home=home;
		}
		public function dispose():void{
			_home=null ;
			angel=0;
			_isDead=false;
			_isPlaying=false;
			_blood=0;
			targetPostion=null 
			_isDispose=true;
			if(parent) parent.removeChild(this);
		}
		
		public function release():void{
			if(_isDispose) return;
			antPool.addObject(this);
		}
		
		public function updata():void{
			if(_isDispose){
			   throw "对象已经回收.";
			}
			if(!_isPlaying) return ;
		    this.x-=_speed * Math.cos(angel);
			this.y-=_speed * Math.sin(angel);
			var distance:Number = Math.sqrt(Math.pow(x-targetPostion.x,2)+Math.pow(y-targetPostion.y,2));
			if(distance<=10){
				_isPlaying=false;
			}
		}
		
		public function attack(v:Number):void{
		    _blood-=v;
			if(_blood<=0){
			   _isDead=true; 
			}
		}
		private var _isDead:Boolean;
		public function checkCastleHit():Boolean{
			var b:Boolean = this.hitTestObject(moveTarget);
			if(b){
				moveTarget.addAnt(this);
			}
		    return b;
		}
		public function setAttackTarget(_ant:Ant):void{
		    _ant.attack(1);
		}
		private var moveTarget:Castle;
		public function moveTo(c:Castle):void{
			_isPlaying=true;
			targetPostion = new Point(c.x,c.y);
			angel=Math.atan2((this.y-c.y),(this.x-c.x));
			this.rotation=angel*(180/Math.PI);
			moveTarget=c;
		}
		
		public function stop():void{
			_isPlaying=false;
		}
	}
}